package amex;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;

import javax.swing.JFileChooser;

public class Amex {
	Boolean test = false;
	File path = null;
	HashMap<String, String> cardNumbers = new HashMap<String, String>();
	ArrayList<String> arraytest = new ArrayList<String>();
	String[] prefix, suffix, content;
	Boolean readyToGo = false;
	ArrayList<File> allFiles;
	HashMap<String, String> monthMap;
	String year = "";
	String month = "";
	String dest = "";
	String fs = "/";
	Properties amex = new Properties();

	public Amex() {
		init();
		 setPath();
		 findAllFiles();
		 renameFiles();
	}

	private void init() {
		setCardNumbers();
		convertMonthToStrings();
		standardDownloadExpressions();

	}

	private void standardDownloadExpressions() {
		amex.put("prefix", "Statement_");
		prefix = new String[] { "Statement_" };
		suffix = new String[] {};
		content = new String[] {};
	}

	private void renameFiles() {
		if (readyToGo) {
			for (File f : allFiles) {
				dest = f.getName();
				String substring = dest.substring(prefix[0].length());
				month = this.monthMap.get(substring.subSequence(0, 3)
						.toString());
				year = substring.subSequence(4, 8).toString();
				f.renameTo(new File(f.getParent() + fs + prefix[0] + year + "_"
						+ month + ".pdf"));
			}
			findAllFiles();
			// String cardnumber = cardNumbers.get("200801");
			for (File f : allFiles) {
				dest = f.getName();
				String substring = dest.substring(prefix[0].length());
				System.out.println(substring);
				month = substring.subSequence(5, 7).toString();
				year = substring.subSequence(0, 4).toString();
				String test = new String(year + month);
				String cardnumber = getCardnumber(test);
				f.renameTo(new File(f.getParent() + fs + cardnumber + year
						+ "_" + month + ".pdf"));
			}
		}
	}

	private String getCardnumber(String test) {
		String cardnumber = null;
		for (String s : cardNumbers.keySet()) {
			int result = test.compareTo(s);
			if (result != 0) {
				if (result > 0) {
					if (cardnumber != null) {
						if (cardnumber.compareTo(s) < 0) {
							cardnumber = s;
						}
					} else {
						cardnumber = s;
					}
				}
			} else {
				cardnumber = test;
				break;
			}
		}
		cardnumber = cardNumbers.get(cardnumber);
		return cardnumber;
	}

	private void findAllFiles() {
		if (readyToGo) {
			FilenameFilter filter = new FilenameFilter() {

				@Override
				public boolean accept(File dir, String name) {
					for (String s : prefix) {
						if (name.startsWith(s)) {
							return true;
						}
					}
					for (String s : suffix) {
						if (name.endsWith(s)) {
							return true;
						}
					}
					for (String s : content) {
						if (name.contains(s)) {
							return true;
						}
					}

					return false;
				}
			};
			allFiles = getAllFiles(getAllDirs(path), filter);
			if (test)
				showAllFiles();
		}
	}

	private void showAllFiles() {
		System.out.println("\nListet alle gefilterte Dateien auf\n");
		for (File f : allFiles) {
			System.out.println("\t" + f.getName());
		}
	}

	private ArrayList<File> getAllFiles(ArrayList<File> allDirectories,
			FilenameFilter filter) {
		ArrayList<File> allFiles = new ArrayList<>();
		for (File f : allDirectories) {
			for (File g : (f.listFiles(filter))) {
				allFiles.add(g);
			}
		}
		return allFiles;
	}

	private ArrayList<File> getAllDirs(File path) {
		ArrayList<File> allDir = new ArrayList<>();
		if (path.isDirectory()) {
			allDir.add(path);
			File[] files = path.listFiles();
			for (File f : files) {
				if (f.isDirectory()) {
					allDir.add(f);
					allDir.addAll(getAllDirs(f));
				}
			}
		}
		return allDir;
	}

	private void setCardNumbers() {
		cardNumbers.put("200801", "3750-008242-51000_");
		cardNumbers.put("201104", "3750-008242-52008_");
		cardNumbers.put("201105", "3750-008242-53006_");
		// String test = "201213";
		// String biggest = "";
		// ArrayList<CardNumber> cntest = new ArrayList<CardNumber>();
		//
		// cntest.add(new CardNumber("200801", "3750-008242-51000_"));
		// cntest.add(new CardNumber("201104", "3750-008242-52008_"));
		// cntest.add(new CardNumber("201105", "3750-008242-53006_"));
		// cntest.add(new CardNumber("200802", "3750-008242-51000_"));
		// cntest.add(new CardNumber("201202", "3750-008242-51000_"));
		// String pointer = null;
		// for (CardNumber n : cntest) {
		// String value = n.getChange();
		// int result = test.compareTo(value);
		// if (result != 0) {
		// if (result > 0) {
		// if (pointer != null) {
		// if (pointer.compareTo(value) < 0) {
		// pointer = value;
		// }
		// } else {
		// pointer = value;
		// }
		// }
		// } else {
		// pointer = test;
		// break;
		// }
		// }
		// System.out.println("\n\nwinner: " + pointer);

	}

	private void test1() {
		arraytest.add("200801");
		arraytest.add("201301");
		arraytest.add("201108");
		arraytest.add("201005");
		arraytest.add("201007");
		arraytest.add("201212");
		showArray();
		Collections.sort(arraytest);
		System.out.println("\n");
		showArray();
	}

	private void showArray() {
		for (String s : arraytest) {
			System.out.println(s);
		}
	}

	private void convertMonthToStrings() {
		monthMap = new HashMap<>();
		monthMap.put("Jan", "01");
		monthMap.put("Feb", "02");
		monthMap.put("Mar", "03");
		monthMap.put("Apr", "04");
		monthMap.put("May", "05");
		monthMap.put("Jun", "06");
		monthMap.put("Jul", "07");
		monthMap.put("Aug", "08");
		monthMap.put("Sep", "09");
		monthMap.put("Oct", "10");
		monthMap.put("Nov", "11");
		monthMap.put("Dec", "12");
		if (test) {
			System.out.println("Zeige alle Monatsdaten auf\n");
			for (String s : monthMap.values()) {
				System.out.println("\t" + s);
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Amex();
	}

	private void setPath() {
		// da mein Testfall aufm Desktop lag hab ich es mir durch die folgende
		// Zeilen einfacher gemacht.
		String path = System.getProperty("user.home") + "/Desktop";
		JFileChooser chooser = new JFileChooser();
		// Auswahl des zu scannenden Ordners
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		try {
			chooser.setCurrentDirectory(new File(path));
		} finally {
			int choice = chooser.showOpenDialog(null);
			if (choice == JFileChooser.APPROVE_OPTION) {
				try {
					path = chooser.getSelectedFile().getCanonicalPath();
					this.path = new File(path);
					readyToGo = true;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
